package com.ilop.sthome.ui.activity.config;

import androidx.recyclerview.widget.GridLayoutManager;

import com.example.common.base.BaseBindActivity;
import com.example.common.utils.InputFilterUtil;
import com.example.common.utils.LiveDataBus;
import com.example.common.utils.SpUtil;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.greenDao.RoomBean;
import com.ilop.sthome.ui.adapter.room.RoomChooseAdapter;
import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.RoomDaoUtil;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityConfigGatewayBinding;

import java.util.List;

/**
 * @author skygge
 * @date 2020-01-08.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：配置网关和房间信息
 */
public class ConfigGatewayActivity extends BaseBindActivity<ActivityConfigGatewayBinding> {

    private int mPosition = 0;
    private RoomChooseAdapter mRoomAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_config_gateway;
    }

    @Override
    protected void initView() {
        super.initView();
        InputFilterUtil.setEditTextInhibitInputSpaChat(mContext, mDBind.etGatewayName);
    }

    @Override
    protected void initData() {
        super.initData();
        int gatewaySize = DeviceDaoUtil.getInstance().findAllGateway().size()+1;
        String gateway = getString(R.string.ali_gateway) + gatewaySize;
        mDBind.etGatewayName.setText(gateway);
        getRoomList();
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.CHOOSE_ROOM, Integer.class).observe(this , integer -> {
            this.mPosition = integer;
            mRoomAdapter.setPosition(integer);
        });
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivConfigBack.setOnClickListener(view -> finish());

        mDBind.configGatewayNext.setOnClickListener(view -> {
            String mGatewayName = mDBind.etGatewayName.getText().toString();
            List<RoomBean> mList = RoomDaoUtil.getInstance().findRoomNotEmptyAndDefault();
            String mRoomId = mList.get(mPosition).getRid();
            SpUtil.putString(mContext, CommonId.KEY_GATEWAY, mGatewayName);
            SpUtil.putString(mContext, CommonId.KEY_BIND_ROOM, mRoomId);
            skipAnotherActivity(AddGatewayGuideActivity.class);
            finish();
        });
    }

    private void getRoomList() {
        List<String> mNameList = RoomDaoUtil.getInstance().findRoomName(mContext);
        GridLayoutManager mManager = new GridLayoutManager(mContext, 4);
        mManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int i) {
                return ConfigureUtil.gridLayoutLineNum(mNameList.get(i).length());
            }
        });
        mRoomAdapter = new RoomChooseAdapter(mContext, mNameList);
        mDBind.configRoomList.setLayoutManager(mManager);
        mDBind.configRoomList.setAdapter(mRoomAdapter);
        mRoomAdapter.setPosition(mPosition);
    }
}
